<?php
/**
  * wechat php 
  */
 function path_weixin($return_url){
	 
 
	 
	$sql = "SELECT * FROM ". $GLOBALS['prefix'] ."weixin_config WHERE `id` = 1";
	$wei = $GLOBALS['db'] -> getRow($sql);
	if(!isset($_GET['code'])){
		$encode_uri = urlencode($return_url);
		$f_url ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=$wei[appid]&redirect_uri=$encode_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
		header("location:$f_url");
	}
	if(isset($_GET['code'])){
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wei[appid]&secret=$wei[appsecret]&code=$_GET[code]&grant_type=authorization_code";
		$res = request($url);
		$_res = json_decode($res);
		$openid = $_res->openid;
		$refresh_token = $_res->refresh_token;
		//获取用户基本信息
		$url_2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$wei[appid]&secret=$wei[appsecret]";
		$res_2 = request($url_2);
		$_res_2 = json_decode($res_2);
		$access_token = $_res_2->access_token;
		$url_3 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
		$res_3 = request($url_3);
		$_res_3 = json_decode($res_3);
		
		
		$_res_3->nickname = iconv('UTF-8', 'GBK', $_res_3->nickname);
		$_res_3->nickname = iconv('GBK', 'UTF-8', $_res_3->nickname);

		
		
		if($openid){
			$_SESSION['openids'] = empty($openid) ? '' : $openid;
			$_SESSION['wei_info'] = empty($_res_3) ? '' : $_res_3;
		}
		header("location:$return_url");	
	}else{
		ECHO'error';
	}
}
 function wx_shouquan($return_url){
	$sql = "SELECT * FROM  ". $GLOBALS['prefix']."weixin_config WHERE `id` = 1";
	$wei = $GLOBALS['db'] -> getRow($sql);
	if(!isset($_GET['code'])){
		$encode_uri = urlencode($return_url);
		$f_urls ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=$wei[appid]&redirect_uri=$encode_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		header("location:$f_urls");
	}
	if (isset($_GET['code'])) {
		$urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wei[appid]&secret=$wei[appsecret]&code=$_GET[code]&grant_type=authorization_code";
		$res = request($urls);
		$_res = json_decode($res);
		$refresh_token = $_res->refresh_token;
  	$url_4 = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$wei[appid]&grant_type=refresh_token&refresh_token=$refresh_token";
		$res_4 = request($url_4);
		$_res_4 = json_decode($res_4);
		$openids = $_res_4->openid;
		$access_tokens = $_res_4->access_token;
		$url_5 = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_tokens&openid=$openids&lang=zh_CN";
		$res_5 = request($url_5);
		$_res_5 = json_decode($res_5);
		if($openids){
			$_SESSION['openids'] = empty($openids) ? '' : $openids;
			$_SESSION['wei_infos'] = empty($_res_5) ? '' : $_res_5;
		}
		header("location:$return_url");
	}else{
		ECHO'error';
	}
}
function request($url,$data='')
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		if(!empty($data)){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		}else{
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;  
			curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; 
		}
		$res = curl_exec($ch);	
		curl_close($ch); 
		return $res;
	}
	
?>